package org.khd.gift.exception;

import cn.hutool.core.util.StrUtil;

import java.util.Collection;
import java.util.Objects;

//自定义断言
public class AssertException {
    /**
     * 断言字符串是否为空
     * @param str
     * @param msg
     */
    public static void isNotBlank(String str,String msg) {
        if(StrUtil.isBlank(str)){
            throw new BusinessException(msg);
        }
    }

    public static void isBlank(String str,String msg) {
        if(StrUtil.isNotBlank(str)){
            throw new BusinessException(msg);
        }
    }

    /**
     * 断言对象是否为空
     * @param obj
     * @param msg
     */
    public static void isNull(Object obj,String msg) {
        if(Objects.nonNull(obj)){
            throw new BusinessException(msg);
        }
    }

    public static void isNotNull(Object obj,String msg) {
        if(Objects.isNull(obj)){
            throw new BusinessException(msg);
        }
    }

    /**
     * 断言集合是否为空
     */
    public static void isNotNull(Collection collection, String msg){
        if(collection == null || collection.isEmpty()){
            throw new BusinessException(msg);
        }
    }

    //断言字符串是否相等
    public static void isEq(String str1,String str2, String msg) {
        if(!str1.equals(str2)){
            throw new BusinessException(msg);
        }
    }

    /**
     * ********************************************************************************
     *  将上面的方法改造成responseCode,使代码更简洁
     * ********************************************************************************
     */
    public static void isEq(String str1,String str2, ResponseCode responseCode) {
        if(!str1.equals(str2)){
            throw new BusinessException(responseCode);
        }
    }
    //*********************************************************************************
    public static void isNotBlank(String str,ResponseCode responseCode) {
        if(StrUtil.isBlank(str)){
            throw new BusinessException(responseCode);
        }
    }

    public static void isBlank(String str,ResponseCode responseCode) {
        if(StrUtil.isNotBlank(str)){
            throw new BusinessException(responseCode);
        }
    }

    /**
     * 断言对象是否为空
     * @param obj
     */
    public static void isNull(Object obj,ResponseCode responseCode) {
        if(Objects.nonNull(obj)){
            throw new BusinessException(responseCode);
        }
    }

    public static void isNotNull(Object obj,ResponseCode responseCode) {
        if(Objects.isNull(obj)){
            throw new BusinessException(responseCode);
        }
    }

    /**
     * 断言集合是否为空
     */
    public static void isNotNull(Collection collection, ResponseCode responseCode){
        if(collection == null || collection.isEmpty()){
            throw new BusinessException(responseCode);
        }
    }

    public static void isNull(Collection collection, ResponseCode responseCode){
        if(!collection.isEmpty()){
            throw new BusinessException(responseCode);
        }
    }

    public static void isTrue(Boolean suc, ResponseCode responseCode){
        if(!suc){
            throw new BusinessException(responseCode);
        }
    }

    public static void isEqValue(String imageCode,String aliyunImageCode,ResponseCode responseCode){
        if(!imageCode.equals(aliyunImageCode)){
            throw new BusinessException(responseCode);
        }
    }

    //判断是否过期
    public static void isExpire(Long expireTime,ResponseCode responseCode){
        //240秒
        if (expireTime > 4 * 60) {
            throw new BusinessException(responseCode);
        }
    }


}
